package com.xplay.xpocker.room.mahjong.action;

import com.xplay.xpocker.constant.MessageNoticeEnum;
import com.xplay.xpocker.dvo.AbstractRoom;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongLogs;
import com.xplay.xpocker.room.IarMessageTL;
import com.xplay.xpocker.room.RoomInfoTL;
import com.xplay.xpocker.room.mahjong.AbstractMahjongMessageStrategy;
import com.xplay.xpocker.room.mahjong.AbstractIsolationEventStrategy;
import com.xplay.xpocker.room.mahjong.MahjongActionHandler;
import com.xplay.xpocker.room.mahjong.constant.MKey;
import com.xplay.xpocker.room.mahjong.dto.*;
import com.xplay.xpocker.room.message.MessageContent;
import com.xplay.xpocker.room.message.MessageContentFactory;
import com.xplay.xpocker.room.message.MessageToClient;
import com.xplay.xpocker.room.message.MessageType;
import com.xplay.xpocker.util.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class HuAction extends AbstractMahjongMessageStrategy<AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs>, MahjongMessage> {
    @Autowired
    private MahjongActionHandler mahjongActionHandler;


    @Override
    protected String strategyName() {
        return MahjongActionEnum.HU.getAction();
    }

    // TODO 碰牌需要判断有没有人要胡牌 杠牌优先级低于胡牌

    /**
     * 1. 自摸 自己处理
     * 2. 胡牌 判断是否有三胡牌
     *
     * @param roomInfo
     * @param messageInfo
     * @return
     */
    @Override
    public boolean doMessage(AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs> roomInfo, MahjongMessage messageInfo) {
        // 调用结算事件
        AbstractIsolationEventStrategy settlementStrategy = AbstractIsolationEventStrategy.getSettlementStrategy(roomInfo.getType());
        MahjongRoomInfo gameInfo = roomInfo.getGameInfo();
        GameRuMahjongUserState currentUser = gameInfo.getCurrentUser();
        Integer winCard = null;
        if (currentUser.allHandCards().size() % 3 == 2) {
            // 自摸  自己胡牌
            currentUser.setWin(true);
            winCard = currentUser.getTouchCard();
            currentUser.setWinCard(winCard);
            currentUser.setWinNumber(currentUser.getWinNumber() + 1);
            // 构建消息通知
            List<MessageContent<MessageToClient>> huMessageList = MessageContentFactory.init(roomInfo.getCode(), MessageType.ACTION.getType(), currentUser.getUserId(), MessageNoticeEnum.ALL).multi(MKey.exeActionUid, currentUser.getUserId(), MKey.cardCode, winCard, MKey.type, MahjongActionEnum.HU.getAction()).send().getMessageList();
            RoomInfoTL.addMessageToClient(huMessageList);
            settlementStrategy.settlementHu(currentUser, null, null, roomInfo);
        } else {
            // 只要有人做胡牌操作   那么那些碰杠  就没有存在的意义了
            cleanBMAction();
            // 只需要判断待处理 有没有  胡牌的
            boolean loading = checkWaitCompletedExistsAction(MahjongActionEnum.HU);
            // 存在其它人  还存在胡牌   这里只能设置用户需要处理
            if (loading) {
                currentUser.saveNextAction(MahjongActionEnum.HU, messageInfo);
                return false;
                //  处理中事件不为空,然后当前人又没加入处理中事件
            } else if (currentUser.getCompleted() == null && ObjectUtils.notEmpty(getAllCompletedData())) {
                currentUser.saveNextAction(MahjongActionEnum.HU, messageInfo);
                // 统一处理所有胡牌事件
                return doLoadingCompleted(roomInfo);
            }
            currentUser.setWin(true);
            winCard = roomInfo.getGameInfo().getExportCard();
            currentUser.setWinCard(winCard);
            currentUser.setCompletedData(null);
            currentUser.setWinUser(roomInfo.getGameInfo().getExportUserId());
            currentUser.setWinNumber(currentUser.getWinNumber() + 1);
            settlementStrategy.settlementHu(currentUser, getUserByUidOrSeq(roomInfo.getGameInfo().getExportUserId(), null), roomInfo.getGameInfo().getExportCard(), roomInfo);

            // 构建消息通知
            List<MessageContent<MessageToClient>> huMessageList = MessageContentFactory.init(roomInfo.getCode(), MessageType.ACTION.getType(), currentUser.getUserId(), MessageNoticeEnum.ALL).multi(MKey.exeActionUid, currentUser.getUserId(), MKey.cardCode, winCard, MKey.type, MahjongActionEnum.HU.getAction()).send().getMessageList();
            RoomInfoTL.addMessageToClient(huMessageList);
        }
        List<GameRuMahjongUserState> allWinUser = getAllWinUser();
        if (allWinUser != null && allWinUser.size() == 1) {
            switchBanker(roomInfo, currentUser.getUserId());
        }
        // 获取当前消息待处理数   doLoadingCompleted   是否存在连续的消息处理
        if (IarMessageTL.get() > 0) {
            return true;
        }
        //判断是否还需要等待 事件执行  发牌
        List<GameRuMahjongUserState> allWAUser = getAllCompletedData();
        if (ObjectUtils.isEmpty(allWAUser) && notWinUserCount() >= 2) {
            dealCard(currentUser.getSeq(), true);
        } else if (notWinUserCount() >= 2) {
            dealCard(currentUser.getSeq(), true);
        } else {
            nextGaming();
        }
        return true;
    }

}
